// pages/in.js
var startPoint;
const min = 0;
const max = 220;
let _this = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    open:false,
    type: '主驾',
    buttonLeft: 0, //座椅左边距
    progress: 0, //座椅进度 0-220
    buttonTop: 0, //靠背上边距
    progress2: 0, //靠背进度 0-220
    //主驾驶参数
    zbuttonLeft: 110, //座椅左边距
    zprogress: 110, //座椅进度 0-220
    zbuttonTop: 110, //靠背上边距
    zprogress2: 110, //靠背进度 0-220
    zopen:false,//主驾同步按钮
    //副驾驶参数
    fbuttonLeft: 110, //座椅左边距
    fprogress: 110, //座椅进度 0-220
    fbuttonTop: 110, //靠背上边距
    fprogress2: 110, //靠背进度 0-220
    fopen:false,//副驾同步按钮

    maxProgress: 10, //最大值0-10
    currentProgress: 5, //默认值0-10
    dis: 13, //吸附距离

    zhufu: 0, // 主驾 副驾驶标志位 0:为主驾驶；1：为副驾驶

    // 蓝牙参数
    connected: false,
    name: '',
    connectedDeviceId: '',
    services: {},
    characteristics: {},
  },
//   切换开关
  switchOpen(e){
     console.log(this.data.open) 
    this.setData({
        open:!this.data.open
    })
    if (this.data.zhufu == 0) {
        this.setData({
            zopen:this.data.open
        })
      }
      if (this.data.zhufu == 1) {
        this.setData({
          fopen:this.data.open
        })
      }
      console.log(this.data) 
  },
  // 靠背拖动相关
  buttonStart2: function (e) {
    startPoint = e.touches[0]
  },
  buttonMove2: function (e) {

    var endPoint = e.touches[e.touches.length - 1]
    var translateY = endPoint.clientY - startPoint.clientY
    var that = this

    startPoint = endPoint;
    var buttonTop = this.data.buttonTop + translateY;
    if (buttonTop > max) {
      return
    }
    if (buttonTop < min) {
      return
    }
    buttonTop = parseInt(buttonTop)
    this.setData({
      buttonTop: buttonTop,
      progress2: buttonTop - min
    })
    if (that.data.zhufu == 0) {
      this.setData({
        zbuttonTop: buttonTop,
        zprogress2: buttonTop - min
      })
    }
    if (that.data.zhufu == 1) {
      this.setData({
        fbuttonTop: buttonTop,
        fprogress2: buttonTop - min
      })
    }
      // 添加联动
      if (that.data.open) {
        let buttonTop1 = 220-buttonTop;
        this.setData({
            buttonLeft: buttonTop1,
            progress: buttonTop1 - min
          })
          if (that.data.zhufu == 0) {
            this.setData({
              zbuttonLeft: buttonTop1,
              zprogress: buttonTop1 - min
            })
          }
          if (that.data.zhufu == 1) {
            this.setData({
              fbuttonLeft: buttonTop1,
              fprogress: buttonTop1 - min
            })
          }
          
      }
    console.log("kaobei", that.data.progress2)
    // if (buttonTop % 10 == 0) {

    //   if (that.data.connected) {
    //     var buffer = new ArrayBuffer(9)
    //     var dataView = new Uint8Array(buffer)
    //     dataView[0] = 0x7B;
    //     dataView[1] = 0x02;
    //     dataView[2] = 0x02;
    //     dataView[3] = that.data.progress2; //靠背加热与通风值设置：
    //     dataView[4] = that.data.progress; //靠背加热与通风值设置：
    //     dataView[5] = that.data.zhufu; //主驾 副驾驶标志位 0:为主驾驶；1：为副驾驶；
    //     dataView[6] = 0xFF;
    //     dataView[7] = 0xFF;
    //     dataView[8] = 0xEF;

    //     console.log('deviceid =', that.data.connectedDeviceId);
    //     console.log('serviceId =', that.data.services[1].uuid);
    //     console.log('characteristics =', that.data.characteristics[0].uuid);
    //     console.log('inputText =', that.data.inputText);
    //     wx.writeBLECharacteristicValue({
    //       deviceId: that.data.connectedDeviceId,
    //       serviceId: that.data.services[1].uuid,
    //       characteristicId: that.data.characteristics[0].uuid,
    //       value: buffer,
    //       success: function (res) {
    //         console.log('发送成功')
    //       },
    //       fail: function (res) {
    //         console.log(res)
    //       }
    //     })
    //   } else {
    //     wx.showModal({
    //       title: '提示',
    //       content: '蓝牙已断开',
    //       showCancel: false,
    //       success: function (res) {
    //         that.setData({
    //           searching: false
    //         })
    //       }
    //     })
    //   }
    // }




  },
  buttonEnd2: function (e) {
    var that = this
    var dis = this.data.dis
    if (this.data.progress2 > (110 - dis) && this.data.progress2 <= (110 + dis)) {
        // 添加联动
      if (that.data.open) {
          this.setData({
            buttonTop: 110,
            progress2: 110,
            buttonLeft: 110, //座椅左边距
            progress: 110, //座椅进度 0-220
          })

      }else{
        this.setData({
            buttonTop: 110,
            progress2: 110,
          })
      }
    }else if(buttonTop < max){
        this.setData({
          buttonTop: buttonTop,
          progress2: buttonTop - min
        })
    }
    console.log('taishou666', e.target.offsetTop);
    var buttonTop = parseInt(e.target.offsetTop)

    if (1 == 1) {
        var kaobei_tongfeng = 0;
        var kaobei_jiare = 0;
        var zuodian_tongfeng = 0;
        var zuodian_jiare = 0;
        //座椅靠背  通风
        if(that.data.progress2 < 97){
            kaobei_jiare = 100 - parseInt(that.data.progress2*100/110);
        }
        //座椅靠背 加热
        if(that.data.progress2 > 123){
            kaobei_tongfeng = 100 - parseInt((220-that.data.progress2)*100/110);
            if(kaobei_tongfeng > 95){
                kaobei_tongfeng = 100;
            }
        }
        //座椅坐垫 通风
        if(that.data.progress < 97){
            zuodian_tongfeng = 100 - parseInt(that.data.progress*100/110);
        }
        //座椅坐垫 加热
        if(that.data.progress > 123){
            zuodian_jiare = 100-parseInt((220-that.data.progress)*100/110);
            if(zuodian_jiare > 95){
                zuodian_jiare = 100;
            }
        }

      if (that.data.connected) {
        var buffer = new ArrayBuffer(9)
        var dataView = new Uint8Array(buffer)
        dataView[0] = 0x7B;
        dataView[1] = 0x02;
        dataView[2] = 0x02;
        dataView[3] = kaobei_tongfeng; //靠背通风值设置：
        dataView[4] = kaobei_jiare; //靠背加热设置：
        dataView[5] = zuodian_tongfeng;//坐垫通风
        dataView[6] = zuodian_jiare;//坐垫加热
        dataView[7] = that.data.zhufu; //主驾 副驾驶标志位 0:为主驾驶；1：为副驾驶；
        dataView[8] = 0xEF;
        console.log('deviceid =888', dataView);

        console.log('deviceid =', that.data.connectedDeviceId);
        console.log('serviceId =', that.data.services[1].uuid);
        console.log('characteristics =', that.data.characteristics[0].uuid);
        console.log('inputText =', that.data.inputText);
        wx.writeBLECharacteristicValue({
          deviceId: that.data.connectedDeviceId,
          serviceId: that.data.services[1].uuid,
          characteristicId: that.data.characteristics[0].uuid,
          value: buffer,
          success: function (res) {
            console.log('发送成功')
          },
          fail: function (res) {
            console.log(res)
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '蓝牙已断开',
          showCancel: false,
          success: function (res) {
            that.setData({
              searching: false
            })
          }
        })
      }
    }
  },
  // 座椅拖动相关
  buttonStart: function (e) {
    startPoint = e.touches[0]
  },
  buttonMove: function (e) {
    var endPoint = e.touches[e.touches.length - 1]
    var translateX = endPoint.clientX - startPoint.clientX
    var that = this
    startPoint = endPoint;
    var buttonLeft = this.data.buttonLeft + translateX;
    if (buttonLeft > max) {
      return
    }
    if (buttonLeft < min) {
      return
    }
    buttonLeft = parseInt(buttonLeft)
    this.setData({
      buttonLeft: buttonLeft,
      progress: buttonLeft - min
    })
    if (that.data.zhufu == 0) {
      this.setData({
        zbuttonLeft: buttonLeft,
        zprogress: buttonLeft - min
      })
    }
    if (that.data.zhufu == 1) {
      this.setData({
        fbuttonLeft: buttonLeft,
        fprogress: buttonLeft - min
      })
    }
    // 添加联动
    if (that.data.open) {
        let buttonLeft1 =220-buttonLeft;
        this.setData({
            buttonTop: buttonLeft1,
            progress2: buttonLeft1 - min
          })
          if (that.data.zhufu == 0) {
            this.setData({
              zbuttonTop: buttonLeft1,
              zprogress2: buttonLeft1 - min
            })
          }
          if (that.data.zhufu == 1) {
            this.setData({
              fbuttonTop: buttonLeft1,
              fprogress2: buttonLeft1 - min
            })
          }
        
    }
    console.log("zuodian", this.data.progress,this.data.buttonLeft,that.data.progress2,this.data.buttonTop)
    // if (buttonLeft % 10 == 0) {
    //   if (that.data.connected) {
    //     var buffer = new ArrayBuffer(9)
    //     var dataView = new Uint8Array(buffer)
    //     dataView[0] = 0x7B;
    //     dataView[1] = 0x02;
    //     dataView[2] = 0x02;
    //     dataView[3] = that.data.progress2; //靠背加热与通风值设置：
    //     dataView[4] = that.data.progress; //靠背加热与通风值设置：
    //     dataView[5] = that.data.zhufu; //主驾 副驾驶标志位 0:为主驾驶；1：为副驾驶；
    //     dataView[6] = 0xFF;
    //     dataView[7] = 0xFF;
    //     dataView[8] = 0xEF;

    //     console.log('deviceid =', that.data.connectedDeviceId);
    //     console.log('serviceId =', that.data.services[1].uuid);
    //     console.log('characteristics =', that.data.characteristics[0].uuid);
    //     console.log('inputText =', that.data.inputText);
    //     wx.writeBLECharacteristicValue({
    //       deviceId: that.data.connectedDeviceId,
    //       serviceId: that.data.services[1].uuid,
    //       characteristicId: that.data.characteristics[0].uuid,
    //       value: buffer,
    //       success: function (res) {
    //         console.log('发送成功')
    //       },
    //       fail: function (res) {
    //         console.log(res)
    //       }
    //     })
    //   } else {
    //     wx.showModal({
    //       title: '提示',
    //       content: '蓝牙已断开',
    //       showCancel: false,
    //       success: function (res) {
    //         that.setData({
    //           searching: false
    //         })
    //       }
    //     })
    //   }
    // }



  },
  buttonEnd: function (e) {
    var that = this
    var dis = this.data.dis
    if (this.data.progress > (110 - dis) && this.data.progress <= (110 + dis)) {
       // 添加联动
       if (that.data.open) {
        this.setData({
          buttonTop: 110,
          progress2: 110,
          buttonLeft: 110, //座椅左边距
          progress: 110, //座椅进度 0-220
        })

    }else{
      this.setData({
            buttonLeft: 110, //座椅左边距
            progress: 110, //座椅进度 0-220
        })
    }
    }else if(buttonTop < max){
        this.setData({
          buttonTop: buttonTop,
          progress2: buttonTop - min
        })
    }
    console.log('taishou6667', e.target.offsetTop);
    var buttonTop = parseInt(e.target.offsetTop)

    if (1 == 1) {
        var kaobei_tongfeng = 0;
        var kaobei_jiare = 0;
        var zuodian_tongfeng = 0;
        var zuodian_jiare = 0;
        //座椅靠背  通风
        if(that.data.progress2 < 97){
            kaobei_jiare = 100 - parseInt((that.data.progress2*100)/110);
        }
        //座椅靠背 加热
        if(that.data.progress2 > 123){
            kaobei_tongfeng = 100 - parseInt(((220-that.data.progress2)*100)/110);
            if(kaobei_tongfeng > 95){
                kaobei_tongfeng = 100;
            }
        }
        //座椅坐垫 通风
        if(that.data.progress < 97){
            zuodian_tongfeng = 100 - parseInt((that.data.progress*100)/110);
        }
        //座椅坐垫 加热
        if(that.data.progress > 123){
            zuodian_jiare = 100 - parseInt(((220-that.data.progress)*100)/110);
            if(zuodian_jiare > 95){
                zuodian_jiare = 100;
            }
        }

      if (that.data.connected) {
        var buffer = new ArrayBuffer(9)
        var dataView = new Uint8Array(buffer)
        dataView[0] = 0x7B;
        dataView[1] = 0x02;
        dataView[2] = 0x02;
        dataView[3] = kaobei_tongfeng; //靠背通风值设置：
        dataView[4] = kaobei_jiare; //靠背加热设置：
        dataView[5] = zuodian_tongfeng;//坐垫通风
        dataView[6] = zuodian_jiare;//坐垫加热
        dataView[7] = that.data.zhufu; //主驾 副驾驶标志位 0:为主驾驶；1：为副驾驶；
        dataView[8] = 0xEF;
        console.log('deviceid =8887', dataView);

        console.log('deviceid =7', that.data.connectedDeviceId);
        console.log('serviceId =7', that.data.services[1].uuid);
        console.log('characteristics =7', that.data.characteristics[0].uuid);
        console.log('inputText =7', that.data.inputText);
        wx.writeBLECharacteristicValue({
          deviceId: that.data.connectedDeviceId,
          serviceId: that.data.services[1].uuid,
          characteristicId: that.data.characteristics[0].uuid,
          value: buffer,
          success: function (res) {
            console.log('发送成功')
          },
          fail: function (res) {
            console.log(res)
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '蓝牙已断开',
          showCancel: false,
          success: function (res) {
            that.setData({
              searching: false
            })
          }
        })
      }
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    _this = this
    this.setData({
      buttonLeft: this.data.currentProgress * (max - min) / this.data.maxProgress + min,
      progress: this.data.currentProgress * (max - min) / this.data.maxProgress,
      buttonTop: this.data.currentProgress * (max - min) / this.data.maxProgress + min,
      progress2: this.data.currentProgress * (max - min) / this.data.maxProgress
    })

    // 蓝牙逻辑增加代码-2023年10月17日

    // 蓝牙连接2022年10月5日
    if (options.name != undefined) {

      console.log(options)
      _this.setData({
        connected: true,
        name: options.name,
        connectedDeviceId: options.connectedDeviceId
      })
      wx.getBLEDeviceServices({
        deviceId: _this.data.connectedDeviceId,
        success: function (res) {
          console.log(res.services)
          _this.setData({
            services: res.services
          })
          wx.getBLEDeviceCharacteristics({
            deviceId: options.connectedDeviceId,
            serviceId: res.services[1].uuid,
            success: function (res) {
              console.log(res.characteristics)
              _this.setData({
                characteristics: res.characteristics
              })
              wx.notifyBLECharacteristicValueChange({
                state: true,
                deviceId: options.connectedDeviceId,
                serviceId: _this.data.services[1].uuid,
                characteristicId: _this.data.characteristics[0].uuid,
                success: function (res) {
                  console.log('启用notify成功')
                }
              })
            }
          })
        }
      })
      wx.onBLEConnectionStateChange(function (res) {
        console.log(res.connected)
        _this.setData({
          connected: res.connected
        })
      })
      wx.onBLECharacteristicValueChange(function (res) {
        var receiveText = app.buf2string(res.value)
        const dataView = new Uint8Array(res.value);
        console.log('_this.data.receiveArray' + dataView);
        console.log('_this.data.receiveArray' + dataView[0]);
        console.log('_this.data.receiveArray' + dataView[1]);
        console.log('_this.data.receiveArray' + dataView[2]);
        console.log('_this.data.receiveArray' + dataView[3]);
        console.log('_this.data.receiveArray' + dataView[4]);
        console.log('_this.data.receiveArray' + dataView[5]);
        var databuf1 = new Uint8Array(res.value);
        var databuf0 = '';
        var receive_over = 0;


      })

    }

  },
  settype(e) {
    console.log("zhufu333", e)
    if (e.currentTarget.dataset.type == "主驾") {
      this.setData({
        type: e.currentTarget.dataset.type,
        progress: this.data.zprogress, //座椅进度 0-220
        progress2: this.data.zprogress2, //靠背进度 0-220
        buttonLeft: this.data.zbuttonLeft,
        buttonTop: this.data.zbuttonTop,
        zhufu: 0,
        open:this.data.zopen,
      })
    }
    if (e.currentTarget.dataset.type == "副驾") {
      this.setData({
        type: e.currentTarget.dataset.type,
        progress: this.data.fprogress, //座椅进度 0-220
        progress2: this.data.fprogress2, //靠背进度 0-220
        buttonLeft: this.data.fbuttonLeft,
        buttonTop: this.data.fbuttonTop,
        zhufu: 1,
        open:this.data.fopen,
      })
    }


    console.log("zhufu", e.currentTarget.dataset.type)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})